// Tested on phpBB
function processElement(element) {
    element.getElementsBySelector("span.postbody").each(function(postbody) {
        var message = postbody.innerHTML;// TODO

        if (message.toLowerCase().indexOf("<span>") != 0)
        message = "<span>" + message + "</span>";

        var dom = message.replace(/\[(.[^\;\]]+);([a-zA-Z0-9\+\/]+)\]/g, '<span class="gwtemplate" title="$1">$2</span>');
        postbody.update(dom);

        postbody.getElementsBySelector("span.gwtemplate").each(function(templateSpan) {
            if (templateSpan.processed) return;

            var templateName = templateSpan.title;
            var templateCode = templateSpan.innerHTML;
            var template = new Gw.Skill.Template(templateName, templateCode);
            templateSpan.update(template.toHtml());
            templateSpan.processed = true;
        });
    });
}

// By default, always process body
processElement($$("body").first());
// Patch BBCodeAjaxPreview
if (typeof BBCodeAjaxPreview != "function") BBCodeAjaxPreview = function() {return false;};
eval(BBCodeAjaxPreview.toString().replace("function BBCodeAjaxPreview", "BBCodeAjaxPreview = function").replace("onSuccess:", "onComplete: function (a) { processElement($('previewBoxContent')); }, onSuccess:"));